<?php
/***
 * Hipay线上充值抽奖
 */
$_GET['mid'] = 'DJEHBSUAAY';

ini_set("display_errors", "On");//开启错误显示
define('INDEX', true);
define('TaskName', 'Recharge_Hipay_Coupon');
require_once dirname(__FILE__) . '/../../Lib/Sing_core.php';
@include libfile('function/api');

DB::SetMainDB(DB_ESHOP);
$crontab = DB::fetch_first("select * from %t where task_name=%s", array('crontab_exec', TaskName));
if ($crontab) {
    $taskStatus = $crontab['task_status'];
    if ($taskStatus) {
        exit;
    } else {
        DB::SetMainDB(DB_ESHOP);
        DB::update('crontab_exec', array(
            'task_status' => 1,
            'task_count' => $crontab['task_count'] + 1,
            'begin_time' => time()
        ), array(
            'task_name' => TaskName
        ));
    }
} else {
    DB::SetMainDB(DB_ESHOP);
    DB::insert('crontab_exec', array(
        'task_name' => TaskName,
        'task_func' => TaskName,
        'task_status' => 1,
        'begin_time' => time(),
        'close_time' => 0,
        'interval_time' => 0,
        'task_count' => 1
    ));
}
DB::SetMainDB(DB_ESHOP);
$rs=DB::fetch_all(" select r.`coupon_id` as coupon_id,r.`member_id` as member_id,r.`id` as coupon_log_id from %t r where r.is_verify=0 and r.status =1 and coupon_id in (4737,4751) and not exists(select * from shechem_hipay_send_coupon_log where coupon_log_id = r.id)",array("es_coupon_log",$starttime,$endtime));
print_r('hipay_send_coupon------------'.serialize($rs));
foreach ($rs as $r) {
    $insert['coupon_id'] = $r['coupon_id'];
    $insert['member_id'] = $r['member_id'];
    $insert['coupon_log_id'] = $r['coupon_log_id'];
    $insert['status'] = 1;
    $insert['inputtime'] = TIME;
    if($r['coupon_id']==4737){
        $insert['couponId'] = 1;
    }else{
        $insert['couponId'] = 2;
    }
    DB::SetMainDB(DB_ESHOP);
    DB::insert("hipay_send_coupon_log", $insert);
}
DB::SetMainDB(DB_ESHOP);
DB::update('crontab_exec', array(
    'task_status' => 0,
    'close_time' => time()
), array(
    'task_name' => TaskName
));

